package com.learnlogincheck.learnlogincheck.contrller;


import com.learnlogincheck.learnlogincheck.daoImpl.loginImpl;
import com.learnlogincheck.learnlogincheck.entity.login;
import com.learnlogincheck.learnlogincheck.utils.RedisUtil;
import com.learnlogincheck.learnlogincheck.utils.Result;
import com.learnlogincheck.learnlogincheck.utils.jwtUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;


@RestController
public class loginContrlller {
    @Autowired
    private loginImpl logins;
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private com.learnlogincheck.learnlogincheck.daoImpl.tockenImpl tockenImpl;
    @CrossOrigin
    @PostMapping(value = "/login")
    @ApiOperation(value = "登录接口",httpMethod = "POST")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "account",required = true,type = "string"),
            @ApiImplicitParam(name = "password",required = true,type = "string")
    })
    public Result show(@RequestParam(value = "account") String account, @RequestParam(value = "password") String password){
        //单shiro
      /*  // 从SecurityUtils里边创建一个 subject
        Subject subject = SecurityUtils.getSubject();
        // 在认证提交前准备 token（令牌）
        UsernamePasswordToken token = new UsernamePasswordToken(account, password);
        // 执行认证登陆
        subject.login(token);
        //根据权限，指定返回数据
        //String role = logins.getRole(account);*/
      //shiro+jwt方式
        Result result=new Result();
        //登录时把生成的token放进redis  失效时间大于token设置失效时间
        List list=new LinkedList();
        String token= jwtUtil.createToken(account);
        //先清空该用户的token
        redisUtil.del(account);
        tockenImpl.deTocken(account);
        //把新的token放进redis
        redisUtil.set(account,token,360);
        tockenImpl.inTocken(account,token);
        login login= logins.show(account,password);
        //list.add(token);
        list.add(login.getAccount());
        list.add(login.getId());
        result.setList(list);
        result.setToken(token);
        return result;
    }
    @RequiresPermissions("vip")
    @GetMapping("/redis")
    public Result showRedis(@RequestHeader(value="Token") String token){
        Result result=new Result();
        String account =jwtUtil.getUsername(token);
        String tockern=redisUtil.get(account).toString();
        result.setToken(tockern);
     return result;
    }

    @GetMapping("/notRole")
    public Result nRole(@RequestParam("message")String message){
        Result result=new Result();
        System.out.println(message+"信息");
        result.setMessage("您没有访问权限！");
        return result;
    }

    @GetMapping("/logout")
    @ApiOperation("注销接口")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "Token",paramType = "String")
    })
    public Result logout(@RequestParam(value = "Token")String token){
        Result result=new Result();
        String account=jwtUtil.getUsername(token);
        redisUtil.del(account);
        tockenImpl.deTocken(account);
        result.setMessage("注销成功");
       return result;
    }
}
